package legumes.client.ui.widgets.list;

import java.util.List;

import legumes.client.ui.widgets.list.item.ListItemWidget;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.UListElement;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Widget;

public class AbstractListWidget extends ComplexPanel {
	
	public AbstractListWidget(Element elt_){
		super();
		setElement(elt_);
	}

	public void setId(String id){
		// Set an attribute common to all tags
		getElement().setId(id);
	}

	public void setDir(String dir){
		// Set an attribute specific to this tag
		((UListElement) getElement().cast()).setDir(dir);
	}

	@Override
	public void add(Widget w) {
		// ComplexPanel requires the two-arg add() method
		super.add(w, getElement());
	}

	public ListItemWidget addSeparator() {
		ListItemWidget separator = new ListItemWidget("|");
		setSeparatorWidgetClassName(separator);
		add( separator );
		return separator;
	}
	
	private void setSeparatorWidgetClassName(ListItemWidget separator) {
		
	}

	public void setMenu(List<Widget> items, boolean withSeparator_)
	{
		// userMenu is an UnorderedListWidget
		clear();
		for (int i=0; i<items.size(); i++)
		{
			Widget itemW = items.get(i);
			if (withSeparator_ && i>0){
				addSeparator();
			}
			
			ListItemWidget liw = new ListItemWidget(itemW);
			setListItemWidgetClassName(liw);
			add(liw);
		}
	}
	
	protected void setListItemWidgetClassName(ListItemWidget liw_){
		
	}
}
